import { defineStore } from "pinia";
import { mainStore } from "@/store/main.js";
import { getDevice, preGetMaintenance } from "@/service/api/maintenance.js";
export const useMaintenance = defineStore("maintenance", {
    state: () => {
        return {
            treeData: [],
            reportTreeData: [],
            checkedItem: [],
            handleItem: [],
            treeSelectNode: {},
            expanded: [],
            stateOperation: 0,
            loading: false
        };
    },
    actions: {
        getDeviceTree(orderId, callBack) {
            this.loading = true;
            getDevice({ id: orderId })
                .then((res) => {
                    this.loading = false;
                    if (res.code === 200) {
                        recursionData(res.data);
                        this.treeData = res.data;
                        this.expanded = [this.treeData[0].id];
                        callBack();
                        function recursionData(arr) {
                            for (let i = 0; i < arr.length; i++) {
                                if (arr[i].depth === 1) {
                                    arr[i].items.forEach((item) => {
                                        item.isRadio = item.state;
                                        item.parentName = arr[i].name;
                                    });
                                    arr[i].isShowCross = arr[i].items.every((item) => !item.isRadio && item.isRadio !== null);
                                    arr[i].isShowChoose = arr[i].items.some((item) => item.isRadio === null);
                                    arr[i].hot = arr[i].items.filter((item) => item.isRadio).length;
                                }
                                if (!arr[i].childs?.length) continue;
                                recursionData(arr[i].childs);
                            }
                        }
                    }
                })
                .catch(() => (this.loading = false));
        },
        async preGetMaintenanceFn(data) {
            const mStore = mainStore();
            const res = await preGetMaintenance({ structureId: data.Id, orderId: mStore.tableData.Id });
            if (res.code === 200) {
                if (!res.data.length) {
                    data.isShowChoose = false;
                    data.isShowCross = true;
                } else {
                    res.data.forEach((item) => {
                        item.isRadio = "";
                    });
                    data.tableData = res.data;
                    data.tableData.forEach((item) => {
                        item.dsposalMethod = item.disposalMethodText?.split(";").map((item) => ({ label: item, value: item }));
                        item.inspectionResult = item.inspectionResultText?.split(";").map((item) => ({ label: item, value: item }));
                    });
                }
                this.treeData[0].childs.every((item) => item.isShowCross) ? (this.treeData[0].isShowCross = true) : (this.treeData[0].isShowCross = false);
                this.treeData[0].childs.some((item) => item.isShowChoose) ? (this.treeData[0].isShowChoose = true) : (this.treeData[0].isShowChoose = false);
            }
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                paths: ["stateOperation"]
            }
        ]
    }
});
